<?php
namespace App\Common\Lib\Hyperf;

use App\Common as com;
use App\Common\Lib\Hyperf\WebSocketClient as thisWebSocketClient;
use Hyperf\HttpMessage\Uri\Uri;
use Hyperf\Utils\Str;

class WebSocketClient extends com\Lib\LibBase {

	public static function create(string $uri, bool $autoClose = true):WebSocketClient\Client {
		if (! Str::startsWith($uri, ['ws://', 'wss://'])) {
			$uri = 'ws://' . $uri;
		}
		$client = \Hyperf\Support\make(WebSocketClient\Client::class, ['uri' => new Uri($uri)]);
		if ($autoClose) {
			defer(function () use ($client) {
				$client->close();
			});
		}
		return $client;
	}

}
